YAHOO.namespace('springmole');

(function () {

    YAHOO.springmole.QueryPanel = function (el, userConfig) {
        YAHOO.springmole.QueryPanel.superclass.constructor.call(this, el, userConfig);
    };
    
    var Event = YAHOO.util.Event, 
		Element = YAHOO.util.Element,
		Dom = YAHOO.util.Dom,
		HttpManager = YAHOO.springmole.HttpManager,
		
		FORM_ACTION = 'databasequery.spy',
		
		BOTTON_ARRAY = [];
    
	
	function createConnectionForm(p_oConnection, p_sQuery, p_oPaginator) {
		var formElement = new YAHOO.springmole.FormElement();

		formElement.addInput('url', 'hidden', p_oConnection.url);
		formElement.addInput('userName', 'hidden', p_oConnection.userName);
		formElement.addInput('password', 'hidden', p_oConnection.password);
		
		formElement.addInput('query', 'hidden', p_sQuery);

		formElement.addInput('sortField', 'hidden', "id");
		formElement.addInput('sortOrder', 'hidden', "asc");

		formElement.addInput('rowIndex', 'hidden', p_oPaginator.getStartIndex());
		formElement.addInput('pageSize', 'hidden', p_oPaginator.getRowsPerPage());
		
		
		return formElement.buildForm('name', 'action').get('element');
	}	
	
	
    function onExecuteButtonClick(p_oMouseEvent, p_oThis) {
		var form = createConnectionForm(
			this.cfg.getProperty(this.CONFIG.CONNECTION.key),
			this._textareaElement.get('element').value,
			this._paginator);
		
		YAHOO.util.Connect.setForm(form);
        YAHOO.util.Connect.asyncRequest('POST', FORM_ACTION, HttpManager.createYUIConnectCallBack({
            success: function (response, resultDataElement) {
                alert('success');
            },
            error: function (response) {
                alert('error');
            },
            failure: function (response) {
                alert('failure');
            },
            argument: {
            }
        }));	
    }
    
    
    function onExportButtonClick(p_oMouseEvent, p_oThis) {
        alert(p_oThis);
    }
    

    function createToolBar(p_sToolbarId) {
        BOTTON_ARRAY[0] = new YAHOO.widget.Button({
            type: "button",
            label: "Execute",
            height: 50,
            container: p_sToolbarId
        });
        BOTTON_ARRAY[0].on('click', onExecuteButtonClick, this, true);
        
        
        BOTTON_ARRAY[1] = new YAHOO.widget.Button({
            type: "menu",
            label: "Options",
            height: 50,
            container: p_sToolbarId,
            menu: [{
                text: 'Test Option 1',
                value: '1'
            }, {
                text: 'Test Option 2',
                value: '2'
            }, {
                text: 'Test Option 3',
                value: '3'
            }, {
                text: 'Test Option 4',
                value: '4'
            }, {
                text: 'Test Option 5',
                value: '5'
            }]
        });
        BOTTON_ARRAY[1].on('click', onExportButtonClick, this, true);
    }
    
    
    YAHOO.lang.extend(YAHOO.springmole.QueryPanel, YAHOO.springmole.SQLDataPanel, {
    
        _dataTableId: null,
        
        _textareaElement: null,
        
        
        initDefaultConfig: function () {
            YAHOO.springmole.QueryPanel.superclass.initDefaultConfig.call(this);
        },
        
        render: function (parentNode) { 
            YAHOO.springmole.QueryPanel.superclass.render.call(this, parentNode);
            
            var toolbarId, layoutId;
            
            toolbarId = Dom.generateId();
            layoutId = Dom.generateId();
            this._dataTableId = Dom.generateId();
            
            this.setHeader('Test Panel');
            this.setBody('<div id="' + toolbarId + '" class="' + this.TOOL_BAR_BUTTON_CLASS + '"></div><div id="' + layoutId + '"></div>');
            
            Event.onAvailable([layoutId], function () {
            
                createToolBar.call(this, toolbarId);
                
                var layout = new YAHOO.widget.Layout(layoutId, {
                    height: 300,
                    width: 480,
                    units: [{
                        position: 'center',
                        body: '',
                        gutter: '0 0 0 0',
                        scroll: false,
                        resize: true
                    }, {
                        position: 'bottom',
                        height: 25,
                        body: '<div id="' + this._dataTableId + '">data table</div>',
                        gutter: '6 0 0 0',
                        resize: true
                    }]
                });
                
                layout.on('render', function () {
                    //var bottomUnit = layout.getUnitByPosition('bottom');
                    var centerUnit;
                    
                    this._textareaElement = new Element(document.createElement('textarea'));
                    
                    Dom.setStyle(this._textareaElement, 'width', '100%');
                    Dom.setStyle(this._textareaElement, 'height', '100%');
                    
                    centerUnit = layout.getUnitByPosition('center');
                    centerUnit.body.appendChild(this._textareaElement.get('element'));
                }, this, true);
                
                layout.render();
                
                
                this._paginator.subscribe('changeRequest', function (state) {                    
                    // Update the Paginator's state, confirming change
                	this._paginator.setState(state);
                }, this, true);
				this._paginator.render();
				
                this._panelResize.on('resize', function (args) {
                    var panelHeight = args.height;
                    
                    layout.set('height', (panelHeight - 100));
                    layout.set('width', (args.width - 20));
                    layout.resize();
                }, this, true);
                
            }, this, true);
            
        } // render
        
    }); // extend Panel
    
}());
